
package eti.vilas.beans;

import java.sql.SQLException;

public class Main
{

   public static void main(String[] args) throws Exception
   {

      DBConnection dbConnection = DBConnection.getInstance();

//      inserirPrimeiro(dbConnection);
      popularBanco(dbConnection, 11);

      for (Abast ab : dbConnection.getAll())
      {
         System.out.println(ab);
      }

      System.out.println();

      System.out.println(dbConnection.getUltimoAbastByVeiculo("HNI1772"));

      System.out.println();

      dbConnection.printAbastTable();

//      apagaTudo(dbConnection);

      dbConnection.close();

   }

   public static void popularBanco_ant(DBConnection dbConnection, int quantidade) throws SQLException, ClassNotFoundException
   {
      for (int i = 0; i < quantidade; i++)
      {
         Abast abastLast = dbConnection.getUltimoAbastByVeiculo("HNI1772");

         Abast abast = new Abast();
         abast.setCnh(726809869);
         abast.setPlaca("hni1772");
         abast.setVrLitro(gerarValorDoLitro());
         abast.setLitros(gerarLitros());
         abast.setVrTotal(abast.getVrLitro() * abast.getLitros());
         abast.setKmStart(abastLast.getKmEnd());
         abast.setKmEnd(abast.getKmStart() + gerarKm());
         abast.setCombustivel(gerarCombustivel());

         dbConnection.addAbst(abast);
      }

   }
   
   public static void popularBanco(DBConnection dbConnection, int quantidade) throws SQLException, ClassNotFoundException
   {
      for (int i = 0; i < quantidade; i++)
      {
         
         Abast abastLast = dbConnection.getUltimoAbastByVeiculo("HNI1772");
         abastLast.setKmEnd(abastLast.getKmStart() + gerarKm());
         dbConnection.atualizaAbast(abastLast);
         
         Abast abastNew = new Abast();
         abastNew.setCnh(726809869);
         abastNew.setPlaca("hni1772");
         abastNew.setVrLitro(gerarValorDoLitro());
         abastNew.setLitros(gerarLitros());
         abastNew.setVrTotal(abastNew.getVrLitro() * abastNew.getLitros());
         abastNew.setKmStart(abastLast.getKmEnd());
         abastNew.setKmEnd(abastLast.getKmEnd());
         abastNew.setCombustivel(gerarCombustivel());
         
         dbConnection.addAbst(abastNew);
         
      }
      
   }

   public static Abast inserirPrimeiro(DBConnection dbConnection) throws SQLException, ClassNotFoundException
   {
      Abast abast = new Abast();
      abast.setCnh(726809869);
      abast.setPlaca("hni1772");
      abast.setVrLitro(gerarValorDoLitro());
      abast.setLitros(gerarLitros());
      abast.setVrTotal(abast.getVrLitro() * abast.getLitros());
      abast.setKmStart(48001);
      abast.setKmEnd(48001);
      abast.setCombustivel(gerarCombustivel());

      dbConnection.addAbst(abast);

      return abast;
   }

   public static void apagaTudo(DBConnection dbConnection) throws SQLException, ClassNotFoundException
   {
      int quantidade = 0;

      for (Abast abast : dbConnection.getAll())
      {
         quantidade += dbConnection.removeAbast(abast);
      }

      System.out.println("Registro apagados: " + quantidade);

   }

   private static String gerarCombustivel()
   {
      Combustivel c[] = Combustivel.values();
      return c[getRandon(c)].toString();
   }

   private static Integer gerarKm()
   {
      Integer[] k = {100, 200, 300, 400, 450, 480, 520};
      return k[getRandon(k)];
   }

   private static Integer gerarLitros()
   {
      Integer[] l = {10, 20, 30, 40, 45, 48, 52};
      return l[getRandon(l)];
   }

   private static float gerarValorDoLitro()
   {
      Float[] v = {2.802F, 2.790F, 1.991F, 2.011F};
      return v[getRandon(v)];
   }

   private static <E> int getRandon(E[] c)
   {
      return (int) (Math.random() * c.length) % c.length;
   }

}
